/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef REALMD_NET_CONNECTIONMANAGER_H_
#define REALMD_NET_CONNECTIONMANAGER_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <boost/unordered_map.hpp>
#include "Ucop/Macros.h"
#include "Ucop/Singleton.h"
#include "Ucop/Policies/ThreadingModel.h"
#include "Ucop/System/Mutex.h"
#include "Realmd/Net/AuthTcpConnection.h"
using Ucop::Singleton;
using Ucop::System::Mutex;


class ConnectionManager
{
public:
    ConnectionManager(void);
    ~ConnectionManager(void);

    void Initialize(void);

    void Add(int _Id, AuthTcpConnection* _Conn);
    void MoveAuthed(int _Id);
    void Update(void);
    void Close(void);

private:
    void _UpdateUnauthedConnections(void);
    void _UpdateAuthedConnections(void);
    void _CloseConnections(void);

    typedef boost::unordered_map<int, AuthTcpConnection*> UnauthedConnectionMap;
    typedef boost::unordered_map<std::uint32_t, AuthTcpConnection*> AuthedConnectionMap;

    time_t _MymaxUnauthedTime;
    UnauthedConnectionMap _MyunAuthedConnections;
    AuthedConnectionMap _MyauthedConnections;
    AuthedConnectionMap _MyclosingConnections;
    Mutex _MyunAuthedConnectionsLock;
    Mutex _MyauthedConnectionsLock;
    DISALLOW_COPY_AND_ASSIGN(ConnectionManager);
};

#define sConnMgr    Singleton<ConnectionManager>::Instance()

#endif // REALMD_NET_CONNECTIONMANAGER_H_
